<?php
class C_cabang extends CI_Controller {
	function __construct(){
		parent::__construct();
		checkLogin();
		$this->load->model('m_cabang');
	}
	function index(){
		$task = secure_post('task');
		switch($task){
			case 'LIST' :
				$this->_list();
				break;
			case 'INSERT' :
				$this->_insert();
				break;
			case 'UPDATE' :
				$this->_update();
				break;
			case 'DELETE' :
				$this->_delete();
				break;
			case 'DEACTIVATE' :
				$this->_deactivate();
				break;
			default :
				$this->load->view('app/v_cabang');
		}
	}
	
	function _list(){
		$data['page'] = numeric_post("page");
		$data['rows'] = numeric_post("rows");
		$data['cabang_nama'] = nosql_post("cabang_nama");
		$data['cabang_aktif'] = nosql_post("cabang_aktif");
		echo $this->m_cabang->_list($data);
	}
	
	function _insert(){
		$data = array(
			'cabang_nama' => $this->input->post('cabang_nama'), 
			'cabang_aktif' => $this->input->post('cabang_aktif'), 
			  
			'cabang_insert_user'	=>$_SESSION[SESSION_USERID],
			'cabang_insert_date'=> date("Y-m-d h:i:s"));
		$this->m_cabang->_insert($data);
		echo 1;
	}
	
	function _update(){
		$cabang_id = $this->input->post('cabang_id');
		$data = array(
			'cabang_nama' => $this->input->post('cabang_nama'), 
			'cabang_aktif' => $this->input->post('cabang_aktif'), 
			
			'cabang_update_user'	=> $_SESSION[SESSION_USERID],
			'cabang_update_date'=> date("Y-m-d h:i:s"));
		$this->m_cabang->_update($data,$cabang_id);
		echo 1;
 	}
	
	function _delete(){
		$cabang_id= $this->input->post('cabang_id');
		$this->m_cabang->_delete($cabang_id);
		echo 1;
	}
	
	function _deactivate(){
		$cabang_id= $this->input->post('cabang_id');
		$data = array(
			'cabang_active' => 'n'
		);
		$this->m_cabang->_deactivate($data,$cabang_id);
		echo 1;
	}
}